<?php
// Tim'Roster
// Copyright (c) 2011 Nicolas Nallet <aspgic2@gmail.com>
//
// This file is part of Tim'Roster.
//
// Tim'Roster is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Tim'Roster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

$xml_config = simplexml_load_file('config.xml');

include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/data_access/guild_DA.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/class/Guild_Hall_Room.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/class/Character.php');
require_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/external_libraries/ryzom_api/ryzom_api.php');

class Guild{

    /**
     * (PHP 5)<br/>
     * Save guild in database
     */
    private function save(){
        $guild_DA = new guild_DA();
        $this->id = $guild_DA->save_guild($this);
    }

    
    public function get_member_list_full(){
        $guild_DA = new guild_DA();

        return $guild_DA->get_guild_member_full($this->get_id());
    }


    /**
     * (PHP 5)<br/>
     * Check if a guild exists in the database
     * @param string $api_key
     * @param bool $is_crypted
     * @return bool
     */
    public static function exists($api_key, $is_crypted){
        $pairs = Guild::get_all_api_key();

        $key = $is_crypted ? ryzom_decrypt($api_key) : $api_key;


        if (isset($pairs)){            
            foreach ($pairs as $pair){
                if ($key == ryzom_decrypt($pair[0])){ return true; }
            }
        }

        return false;
    }

    /**
     * (PHP 5)<br/>
     * Get all guild api key in the db. Note that key are crypted
     * @return array of string
     */
    public static function get_all_api_key(){
        $guild_DA = new guild_DA();
        return $guild_DA->get_all_api_key();
    }

    /**
     * (PHP 5)<br/>
     * Get guild api key in the db. Note that key are crypted
     * @return array of string
     */
    public static function get_guild_api_key($guild_id){
        $guild_DA = new guild_DA();
        return $guild_DA->get_guild_api_key($guild_id);
    }

    /**
     * (PHP 5)<br/>
     * Get guild information
     * @param int $id
     * @return  Guild
     */
    public static function get_guild($id){
        $da = new guild_DA();

        $guild = $da->get_guild($id);

        return $guild;
    }

    /**
     * (PHP 5)<br/>
     * Get guild information light (id, name, icon)
     * @param int $id
     * @return  Guild
     */
    public static function get_guild_light($id){
        $da = new guild_DA();

        $guild = $da->get_guild_light($id);

        return $guild;
    }

    /**
     * (PHP 5)<br/>
     * Get primary guild information light (id, name, icon)
     * @param int $id
     * @return  Guild
     */
    public static function get_primary_guild_light(){
        $da = new guild_DA();

        $guild = $da->get_primary_guild_light();

        return $guild;
    }

    /**
     * (PHP 5)<br/>
     * Get guild list
     * @return array of Guild
     */
    public static function get_guild_list(){
        $da = new guild_DA();
        return $da->get_guild_list();
    }



    /**
     * (PHP 5)<br/>
     * Set guild general informations
     * @param int $id
     * @param string $api_key
     * @param string $name
     * @param int $race
     * @param int $icon
     * @param string $shard
     * @param string $description
     * @param date $creation_date
     * @param int $money
     * @param datetime $last_update_date
     */
    public function set_general_information($id, $api_key, $name, $race, $icon, $shard,
            $description, $creation_date, $money,  $last_update_date){

        $this->id = $id;
        $this->api_key = $api_key;
        $this->name = $name;
        $this->race = $race;
        $this->icon = $icon;
        $this->shard = $shard;
        $this->description = $description;
        $this->creation_date = $creation_date;
        $this->money = $money;

        $this->last_update_date = $last_update_date;
    }

    /**
     * (PHP 5)<br/>
     * Set guild fame values
     * @param string $civilization
     * @param string $cult
     * @param int $fyros_fame
     * @param int $matis_fame
     * @param int $tryker_fame
     * @param int $zorai_fame
     * @param int $kami_fame
     * @param int $karavan_fame
     */
    public function set_fame($civilization, $cult, $fyros_fame, $matis_fame, $tryker_fame,
        $zorai_fame, $kami_fame, $karavan_fame){
        $this->civilization = $civilization;
        $this->cult = $cult;
        $this->fyros_fame = $fyros_fame;
        $this->matis_fame = $matis_fame;
        $this->tryker_fame = $tryker_fame;
        $this->zorai_fame = $zorai_fame;
        $this->kami_fame = $kami_fame;
        $this->karavan_fame = $karavan_fame;
    }


    /**
     * (PHP 5)<br/>
     * Deserialize a guild
     * @param SimpleXMLElement $xml
     * @param string $api_key
     * @param int $guild_id
     */
    public function deserialize($xml, $api_key, $guild_id){
        $this->id = $guild_id;
        $this->name = $xml->name;
        $this->race = $xml->race;
        $this->icon = $xml->icon;
        $this->shard = $xml->shard;
        //TODO : display real date, try date of the present date - $xml->creation_date
        $this->creation_date = $xml->creation_date;
        $this->description = $xml->description;
        $this->money = $xml->money;

        /** Fame informations **/
        $this->cult = $xml->cult;
        $this->civilization = $xml->civ;
        $this->fyros_fame = floor($xml->fames->fyros / 6000);
        $this->kami_fame = floor($xml->fames->kami / 6000);
        $this->karavan_fame = floor($xml->fames->karavan / 6000);
        $this->matis_fame = floor($xml->fames->matis / 6000);
        $this->tryker_fame = floor($xml->fames->tryker / 6000);
        $this->zorai_fame = floor($xml->fames->zorai / 6000);

        $this->api_key = $api_key;

        /** Member list **/
        $this->member_list = array();

        $members_xpath = $xml->xpath('/guild/members/*');
        foreach($members_xpath as $m)
        {
            $member = new Guild_Member();
            $member->deserialize($m);
            $this->member_list[] = $member;
        }

        $this->save();

        /** GH chest content **/
        $this->room = new Guild_Hall_Room($this->get_id());
        $this->room->load_new_content($xml);
        $this->room->load_current_content();
        $this->room->update_guild_hall_room_content(($guild_id == null));

    }

    private $id;
    /**
     * (PHP 5)<br/>
     * Get guild id. This id come from the database
     * @return int
     */
    public function get_id(){
        return $this->id;
    }

    private $name;
    /**
     * (PHP 5)<br/>
     * Get guild name
     * @return string
     */
    public function get_name(){
            return $this->name;
    }
    /**
     * (PHP 5)<br/>
     * Set guild name
     * @param string $name
     */
    public function set_name($name){
        $this->name = $name;
    }

    private $api_key;
    /**
     * (PHP 5)<br/>
     * Get guild api key
     * @return string
     */
    public function get_api_key(){
        return $this->api_key;
    }

    private $last_update_date;
    /**
     * (PHP 5)<br/>
     * Get guild last update date
     * @return datetime
     */
    public function get_last_update_date(){
        return $this->last_update_date;
    }

    private $race;
    /**
     * (PHP 5)<br/>
     * Get guild race
     * @return string
     */
    public function get_race(){
            return $this->race;
    }
    private $icon;
    /**
     * (PHP 5)<br/>
     * Get guild icon
     * @return int
     */
    public function get_icon(){
            return $this->icon;
    }
    private $shard;
    /**
     * (PHP 5)<br/>
     * Get guild shard
     * @return string
     */
    public function get_shard(){
            return $this->shard;
    }
    private $creation_date;
    /**
     * (PHP 5)<br/>
     * Get guild creation date
     * @return date
     */
    public function get_creation_date(){
            return $this->creation_date;
    }

    private $description;
    /**
     * (PHP 5)<br/>
     * Get guild description 
     * @return string
     */
    public function get_description(){
            return $this->description;
    }
    private $money;
    /**
     * (PHP 5)<br/>
     * Get guild money
     * @return int
     */
    public function get_money(){
            return $this->money;
    }

    /** Fame informations **/
    private $cult;
    /**
     * (PHP 5)<br/>
     * Get guild cult
     * @return string
     */
    public function get_cult(){
            return $this->cult;
    }
    private $civilization;
    /**
     * (PHP 5)<br/>
     * Get guild civilization
     * @return string
     */
    public function get_civilization(){
            return $this->civilization;
    }
    private $fyros_fame;
    /**
     * (PHP 5)<br/>
     * Get guild fyros fame
     * @return int
     */
    public function get_fyros_fame(){
            return $this->fyros_fame;
    }
    private $kami_fame;
    /**
     * (PHP 5)<br/>
     * Get guild kami fame
     * @return int
     */
    public function get_kami_fame(){
            return $this->kami_fame;
    }
    private $karavan_fame;
    /**
     * (PHP 5)<br/>
     * Get guild karavan fame 
     * @return int
     */
    public function get_karavan_fame(){
            return $this->karavan_fame;
    }
    private $matis_fame;
    /**
     * (PHP 5)<br/>
     * Get guild matis fame
     * @return int
     */
    public function get_matis_fame(){
            return $this->matis_fame;
    }
    private $tryker_fame;
    /**
     * (PHP 5)<br/>
     * Get guild tryker fame
     * @return int 
     */
    public function get_tryker_fame(){
            return $this->tryker_fame;
    }
    private $zorai_fame;
    /**
     * (PHP 5)<br/>
     * Get guild zorai fame
     * @return int
     */
    public function get_zorai_fame(){
            return $this->zorai_fame;
    }

    /** Member list **/
    private $member_list;
    /**
     * (PHP 5)<br/>
     * Get guild member list
     * @return array of Guild_Member
     */
    public function get_member_list(){
        return $this->member_list;
    }
    /**
     * (PHP 5)<br/>
     *  Set guild member list
     * @param array of Guild_Member $member_list
     */
    public function set_member_list($member_list){
        $this->member_list = $member_list;
    }

    /** Guild Hall Chest **/
    private $room;
    /**
     * (PHP 5)<br/>
     * Get guild room
     * @return Guild_Hall_Room
     */
    public function get_room(){
        return $this->room;
    }

    /**
     * (PHP 5)<br/>
     * Get the number of guild member that belong to a grade
     * @param int $grade_id
     * @return int
     */
    public function get_grade_member_count($grade_id){
        $result = 0;
        
        foreach($this->get_member_list() as $member)
        {
            if ($member->get_grade_id() == $grade_id) {
                $result++;
            }
        }

        return $result;
    }

}





?>